﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 文章分页列表。
	/// </summary>
	public class ArticlePagingList : ArticleList
	{
		#region
		/// <summary>
		/// 分页的查询字段。
		/// </summary>
		public virtual string PageField
		{
			get { return GenericUrlManager.Article_Query_PageField; }
		}

		/// <summary>
		/// 分页的列表Url模式。
		/// </summary>
		public virtual string ListUrlPattern
		{
			get { return GenericUrlManager.BuildArticleListUrlPattern(ModuleId); }
		}
		#endregion


		/// <summary>
		/// 分页控件。
		/// </summary>
		protected Wuqi.Webdiyer.AspNetPager CtrPager;

		private LoadedValue<string> loadedModuleCode;
		/// <summary>
		/// 列表的模块代码。
		/// </summary>
		public override string ModuleCode
		{
			get
			{
				if (loadedModuleCode.Loader == null)
					loadedModuleCode.Loader = () => Request.QueryString[GenericUrlManager.Article_Query_ModuleCodeField];
				return loadedModuleCode.Value;
			}
		}

		/// <summary>
		/// 列表的附加模块代码（无效）。
		/// </summary>
		public override string AttachedModuleCodes
		{
			get
			{
				return null;
			}
		}

		private LoadedValue<string> loadedKeyword;
		/// <summary>
		/// 列表的查询关键字。
		/// </summary>
		public override string Keyword
		{
			get
			{
				if (loadedKeyword.Loader == null)
					loadedKeyword.Loader = () => Request.QueryString["Keyword"];
				return loadedKeyword.Value;
			}
		}

		/// <summary>
		/// 列表的更多Url（无效）。
		/// </summary>
		public override string ListMoreUrl
		{
			get
			{
				return null;
			}
		}

		private LoadedValue<SearchType> loadedSearchType;
		/// <summary>
		/// 列表的搜索类型。
		/// </summary>
		public virtual SearchType SearchType
		{
			get
			{
				if (loadedSearchType.Loader == null)
					loadedSearchType.Loader = () => EnumExtensions.ParseOrDefault<SearchType>(Request.QueryString["SearchType"], SearchType.None);
				return loadedSearchType.Value;
			}
		}

		private LoadedValue<int> loadedCurrentPage;
		/// <summary>
		/// 列表的当前页码。
		/// </summary>
		public virtual int CurrentPage
		{
			get
			{
				if (loadedCurrentPage.Loader == null)
					loadedCurrentPage.Loader = () => IntExtensions.ParseOrDefault(Request.QueryString[PageField], 1);
				return loadedCurrentPage.Value;
			}
		}



		protected override void OnInit(EventArgs e)
		{
			CtrPager.UrlPageIndexName = GenericUrlManager.Article_Query_PageField;
			if (SearchType != SearchType.None)
			{
				CtrPager.EnableUrlRewriting = false;
			}
			else
			{
				CtrPager.EnableUrlRewriting = true;
				CtrPager.UrlRewritePattern = ListUrlPattern;
			}
			base.OnInit(e);
		}

		/// <summary>
		/// 获取列表数据。
		/// </summary>
		/// <returns>获取的列表数据。</returns>
		public override object GetListData()
		{
			PaginationInfo<List<Articles.ModuleArticleForClientBaseList>> pageInfo = null;
			pageInfo = Articles.GetPaginationBaseList(SiteId, ModuleId, Keyword, CtrPager.PageSize, CurrentPage);
			CtrPager.RecordCount = pageInfo.RecordCount;
			return pageInfo.Records;
		}

		protected virtual void RewriteMeta()
		{
			this.Page.Title = (ModuleInfo == null ? Res.ArticleList : ModuleInfo.ModuleName) +
				(CurrentPage > 1 ? Hxsoft.AppV1.Page.TitleSectionSeparator + string.Format(Res.TitlePaging, CurrentPage) : "")
				+ Hxsoft.AppV1.Page.TitleSuffix;

            Hxsoft.AppV1.Page page = this.Page as Hxsoft.AppV1.Page;
            if (page != null)		// 更新页面Meta
            {
                if (!string.IsNullOrEmpty(ModuleInfo.Keyword))
                {
                    page.MetaKeywords = ModuleInfo.Keyword;
                }
                if (!string.IsNullOrEmpty(ModuleInfo.Describe))
                {
                    page.MetaDescription = ModuleInfo.Describe;
                }
            }
		}

		protected override void OnPreRender(EventArgs e)
		{
			RewriteMeta();

			base.OnPreRender(e);
		}
	}
}